﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using TEL.TelPhoneF.Dialogs;
using TEL.TelPhoneF.InfoList;

namespace TEL.TelPhoneF.Controls
{
    public partial class DataListContact : TEL.TelPhoneF.Controls.DataListCustom
    {
        private List<InfoList.Contact> Contacts;

        public string SerializeList()
        {
            MemoryStream memoryStream = new MemoryStream();
            XmlSerializer serializer = new XmlSerializer(typeof(List<InfoList.Contact>));
            serializer.Serialize(memoryStream, Contacts);
            UTF8Encoding encoding = new UTF8Encoding();
            string vertexXml = encoding.GetString(memoryStream.ToArray());
            return vertexXml;
        }

        public void DeSerializeList(string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                Contacts = null;
                return;
            }
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(data);
            MemoryStream memoryStream = new MemoryStream(byteArray);
            XmlSerializer serializer = new XmlSerializer(typeof(List<InfoList.Contact>));
            Contacts = new List<InfoList.Contact>((List<InfoList.Contact>)serializer.Deserialize(memoryStream));
        }

        private void ViewContact(Contact contact)
        {
            ListViewItem item = new ListViewItem();
            item.Text = contact.ContactName;
            item.SubItems.Add(contact.ContactPhone);
            item.Tag = contact;
            
            ListItemAdd(item);
        }

        private void ViewContactList(List<InfoList.Contact> contactList)
        {

            foreach (var contact in contactList)
            {
                ViewContact(contact);
            }
        }
        public override void LoadList()
        {
            string contacts = Properties.Settings.Default.Contacts.Trim();
            ClearList();
            if(!string.IsNullOrEmpty(contacts))
            {    try
                {
                    DeSerializeList(contacts);
                    ViewContactList(Contacts);
                }
                catch (Exception)
                {
                }
            }
            else
            {
                Contacts = new List<Contact>();
            }

        }

        protected override void SaveList()
        {
            Contacts.Sort(new Comparison<Contact>((a,b) => a.ContactName.CompareTo(b.ContactName) ));
          
            Properties.Settings.Default.Contacts = SerializeList();
            Properties.Settings.Default.Save();
        }

        protected override void btnAdd_Click(object sender, EventArgs e)
        {
            Contact contact = new Contact();
            Dialogs.DialogContact dialogContact = new DialogContact() {SourceObject = contact};
            if (dialogContact.ShowDialog() == DialogResult.OK)
            {
                Contacts.Add(contact);
                SaveList();
                LoadList();
            }
        }

        protected override void btnEdit_Click(object sender, EventArgs e)
        {
            if (dataList.SelectedItems.Count>0)
            {
                ListViewItem item = dataList.SelectedItems[0];
                Contact contact = item.Tag as Contact;
                if(contact != null)
                {
                    Dialogs.DialogContact dialogContact = new DialogContact() { SourceObject = contact };
                    if (dialogContact.ShowDialog() == DialogResult.OK)
                    {
                        SaveList();
                        LoadList();
                    }
                }
            }
        }

        protected override void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataList.SelectedItems.Count > 0)
            {
                ListViewItem item = dataList.SelectedItems[0];
                Contact contact = item.Tag as Contact;
                if (contact != null)
                {
                    Dialogs.DialogContact dialogContact = new DialogContact() { SourceObject = contact };
                    if (dialogContact.ShowDialog() == DialogResult.OK)
                    {
                        Contacts.Remove(contact);
                        SaveList();
                        LoadList();
                    }
                }
            }
        }
        public DataListContact()
        {
            InitializeComponent();
        }
    }
}
